package com.zt.config;

import com.zt.strategy.MsgHandler;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 配置
 *
 * @author zt
 * @date 2021/12/10 15:42
 */
@Component
public class MsgConfig implements ApplicationContextAware {

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //获取所有策略注解的Bean
        applicationContext.getBeansWithAnnotation(Type.class).
                entrySet().iterator().forEachRemaining(entrySet -> {
                    Class<MsgHandler> baseMsgHandler = (Class<MsgHandler>) entrySet.getValue().getClass();
                    MsgType value = baseMsgHandler.getAnnotation(Type.class).value();
                    // 将class加入MsgHandlerContext的map中,注解的value作为key
                    MsgHandlerContext.msgTypeClassMap.put(value, baseMsgHandler);
                });
    }
}
